<?php
namespace Center\Controller;
use Think\Controller;
/* 移动端对接aws人力平台接口控制器 */
class ApiController extends CommonController{
    /* 属性声明 */
    private $token_cache_time = 1800;
    private $corpid = null;
    private $secret = null;
    private $token = null;
    private $result = null;
    private $msg = '';
    private $code = '';
    //当前 error_code -> 6017
    //消息推送应用id
    private $agentid = 23;
    
    /* 方法声明 */
    public function __construct() {
        parent::__construct();
        if(count($_POST)==1){
            $this -> data = json_decode(array_keys($_POST)[0],true);
        }else{
            $this -> data = $_POST;
        }
        //判断当前方法名称
        if(ACTION_NAME=='generate'){
            $this -> data = base64_decode($this -> data['data']);
            $this -> data = iconv("gb2312","utf-8",$this -> data);
            $this -> data = json_decode($this -> data,true);
        }
    }
    
    /* 生成token */
    public function getToken(){
        $data = I('get.');
        //检测参数是否存在
        if(!$data['corpid'] || !$data['secret']){
            $this -> code = 6001;
            $this -> msg = 'corpid或secret参数缺失';
            $this -> response_token(0);
        }
        //检测正确性
        if($this -> userinfo[$data['corpid']]===null || $this -> userinfo[$data['corpid']]!== $data['secret']){
            $this -> code = 6002;
            $this -> msg = 'corpid或secret参数验证失败';
            $this -> response_token(0);
        }
        //生成token
        $token = 
        md5(time() . $data['corpid'] . $data['secret'] . mt_rand(1111,9999));
        
        //记录客户端ip
        $token_info['corpid'] = $data['corpid'];
        $token_info['expires'] = $this -> token_cache_time;
        $token_info['remote_ip'] = $_SERVER['REMOTE_ADDR'];
        $token_info['token'] = $token;
        
        S($token,$token_info,$this->token_cache_time);
        //成功返回
        $this -> code = 6000;
        $this -> msg = 'ok';
        $this -> token = $token;
        $this ->response_token(1);
    }
    
    /**
     * 获取token返回数据
     * @param $stats int 1 表示获取成功 0 表示获取失败
     * @return string 返回json字符串
     */
    private function response_token($stats){
        $res_data['code'] = $this -> code;
        $res_data['msg'] = $this -> msg;
        if($stats==1){
            $res_data['token'] = $this -> token;
        }
        echo json_encode($res_data,JSON_UNESCAPED_UNICODE);
        exit;
    }
    
    /**
     * 员工入司接口
     */
    public function entry(){
        $data = $this -> data;
        $this -> check_token();
        //获取员工编号
        if(!$data['uuid']){
            $this -> msg = '员工编号缺失';
            $this -> code = 6005;
            $this -> response_result();
        }
        //向企业号添加新成员
        $tool = A('Tool/Corp');
        //获取企业号数据
        $corp_id = $this -> corpid;
        $corp_secret = C('CORPID_LIST')[$corp_id]['secret'];
        A('Tool/Corp') -> init($corp_id,$corp_secret);
        //获取员工姓名
        if(!$uname = $data['name']){
            $this -> msg = '员工姓名缺失';
            $this -> code = 6006;
            $this -> response_result();
        }
        //获取员工手机
        if(!$mobile = $data['mobile']){
            $this -> msg = '员工手机号码缺失';
            $this -> code = 6007;
            $this -> response_result();
        }
        $config['userid'] = $data['uuid'];
        $config['name'] = $uname;
        $config['department'] = 1;
        $config['mobile'] = $mobile;
        $config['uuid'] = $data['uuid'];
        //执行添加成员
        if(A('Tool/Corp') -> createUser($config)){
            $this -> msg = 'ok';
            $this -> code = 6000;
            $this -> response_result();
        }else{
            $this -> msg = A('Tool/Corp') -> getError();
            $this -> code = 6008;
            $this -> response_result();
        }
    }
    
    /**
     * 员工离职接口
     */
    public function dimission(){
        $data = $this -> data;
        $this -> check_token();
        //获取员工编号
        if(!$data['uuid']){
            $this -> msg = '员工编号缺失';
            $this -> code = 6005;
            $this -> response_result();
        }
        $tool = A('Tool/Corp');
        //获取企业号数据
        $corp_id = $this -> corpid;
        $corp_secret = C('CORPID_LIST')[$corp_id]['secret'];
        A('Tool/Corp') -> init($corp_id,$corp_secret);
        //更改成员状态为禁用 redis数据库数据不做删除操作 考虑json结构添加员工状态数据
        $config['userid'] = $data['uuid'];
        $config['enable'] = 0;
        $res = A('Tool/Corp') -> closeUser($config);
        if($res){
            $this -> msg = 'ok';
            $this -> code = 6000;
            $this -> response_result();
        }else{
            $this -> msg = A('Tool/Corp') -> getError();
            $this -> code = 6009;
            $this -> response_result();
        }
    }
    
    /**
     * 检测用户token合法性
     */
    private function check_token(){
        $data = $this -> data;
        //检测token数据是否存在
        if(!isset($data['token'])){
            $this -> msg = 'token验证凭据丢失';
            $this -> code = 6003;
            $this -> response_result();
        }
        //检测token合法性
        $corpid = S($data['token'])['corpid'];
        //检测请求端ip地址
        $original_remote_ip = S($data['token'])['remote_ip'];
        $current_remote_ip = $_SERVER['REMOTE_ADDR'];
        if($original_remote_ip != $current_remote_ip){
            $this -> msg = '拒绝可疑的token请求';
            $this -> code = 6017;
            $this -> response_result();
        }
        $this -> corpid = $corpid;
        if(!$corpid){
            $this -> msg = 'token非法或失效过期';
            $this -> code = 6004;
            $this -> response_result();
        }
        return true;
    }
    
    /**
     * 功能结构返回数据
     */
    private function response_result(){
        $res_data['code'] = $this -> code;
        $res_data['msg'] = $this -> msg;
        echo json_encode($res_data,JSON_UNESCAPED_UNICODE);
        exit;
    }
    
    /**
     * 通用消息接口
     */
    public function notice(){
        $data = $this -> data;
        $this -> check_token();
        
        //获取员工编号
        if(!$data['uuid']){
            $this -> msg = '员工编号缺失';
            $this -> code = 6005;
            $this -> response_result();
        }
        //获取标题
        if(!$data['title']){
            $this -> msg = '标题缺失';
            $this -> code = 6010;
            $this -> response_result();
        }
        //获取正文
        if(!$data['content']){
            $this -> msg = '消息主体缺失';
            $this -> code = 6011;
            $this -> response_result();
        }
        $data['agentid'] = $this -> agentid;
        
        $tool = A('Tool/Corp');
        //获取企业号数据
        $corp_id = $this -> corpid;
        $corp_secret = C('CORPID_LIST')[$corp_id]['secret'];
        A('Tool/Corp') -> init($corp_id,$corp_secret);
        $res = A('Tool/Corp') -> sentMsg($data);
        if($res){
            $this -> msg = 'ok';
            $this -> code = 6000;
            $this -> response_result();
        }else{
            $this -> msg = A('Tool/Corp') -> getError();
            $this -> code = 6012;
            $this -> response_result();
        }
    }
    
    /**
     * 预警消息接口
     */
    
    /**
     * 员工入司json结构体写入接口
     */
    public function generate(){
        $data = file_get_contents("php://input");

        $data = json_decode($data,true);
        $this -> data = $data;
        $this -> check_token();
        //验证post数据合法性
        if(!$data['uuid'] || !$data['data']){
            $this -> msg = '缺失接口必须参数';
            $this -> code = 6016;
            $this -> response_result();
        }
        
        //检测json体格式是否正确 暂作粗略验证
        $json_data =  $data['data'];
        
        //写入redis
        $redis = connectRedis();
        $uuid = $data['uuid'];
        $json = $data['data'];
        $redis -> select(0);
        $res = $redis -> hset('user_list',$uuid,  json_encode($json_data,JSON_UNESCAPED_UNICODE ));
        $this -> msg = 'ok';
        $this -> code = 6000;
        $this -> response_result();
    }
    
    public function test(){
        $data = file_get_contents("php://input");
        file_put_contents('rr.rr', $data);
        echo $data;die;
    }
    
    /**
     * __call魔术方法
     */
    public function __call($func_name,$args){
        $this -> msg = '非法接口调用'.$func_name;
        $this -> code = 6018;
        $this -> response_result();
    }
}
